package comm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
import java.util.Map;


public class EZConnection {

	private URL url;
	private HttpURLConnection conn;
    private Method method;

    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;
	
	public enum Method {
		GET, POST
	}
	
	/**
	 * @param url new URL
	 */
	public EZConnection(URL url){
		this.url = url;
	}

	/**
	 * Open a new connection.
	 * @throws java.io.IOException
	 */
	public void open() throws IOException{
		conn = (HttpURLConnection) this.url.openConnection();
	}
	
	/**
	 * Close the connection.
	 */
	public void close() throws IOException {
        if (bufferedReader != null)
            bufferedReader.close();
        if (bufferedWriter != null)
            bufferedWriter.close();
		conn.disconnect();
	}
	
	/**
	 * Find all the parameters used by current connection.
	 * @param key the param to find
	 * @return current connection properties
	 */
	public String getProperty(String key){
		return conn.getRequestProperty(key);
	}
	
	/**
	 * @return the actual transfer method
	 */
	public String getMethod(){
		return conn.getRequestMethod();
	}
	
	/**
	 * Set the transfer method.
	 * @param method the transfer method.
	 * @throws java.net.ProtocolException
	 */
	public void setMethod(Method method) throws ProtocolException{
		switch(method){
			case GET : 
				conn.setRequestMethod("GET"); 
				break;
			case POST :
				conn.setDoOutput(true);
				conn.setDoInput(true);
				conn.setRequestMethod("POST");
				break;
			default :
				throw new ProtocolException("Invalid Protocol.");
		}
		this.method = method;
	}
	
	/**
	 * @return current response code
	 * @throws java.io.IOException
	 */
	public int getResponseCode() throws IOException{
		return conn.getResponseCode();
	}
	
	/**
	 * @param key key value
	 * @param value new parameter
	 */
	public void setProperty(String key, String value){
		conn.setRequestProperty(key, value);
	}

	/**
	 * @return current connection properties
	 */
	public Map<String,List<String>> getProperties(){
		return conn.getRequestProperties();
	}

    /**
     * @return current connection content from service.
     * @throws IOException
     */
	public String getContent() throws IOException{
		if (getResponseCode() != 200)
			throw new IOException("Error: " + getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String str;
        StringBuilder sb = new StringBuilder();
		while ((str = br.readLine()) != null)
			sb.append(str);
        return sb.toString();
	}

    public BufferedReader getInputReader() throws IOException{
        bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        return bufferedReader;
    }

    public BufferedWriter getOutputWriter() throws IOException{
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
        return bufferedWriter;
    }
	
}
